import { createApp, ref, ComponentPublicInstance } from "vue";
import SearchDialog from "./SearchDialog.vue";
export default {
  install() {
    const modalRef = ref<ComponentPublicInstance<typeof SearchDialog> | null>(null);
    const setShow = (value: boolean) => {
      modalRef.value?.setShow(value);
    };
    // 注册全局方法
    window.$setSearchDialog = setShow;

    // 添加键盘事件监听器
    const handleKeyDown = (event: KeyboardEvent) => {
      if (event.ctrlKey && event.key === "k") {
        event.preventDefault();
        let state = modalRef.value?.getState();
        setShow(!state); // 显示弹窗
      }
    };

    // 监听键盘事件
    window.addEventListener("keydown", handleKeyDown);

    // 挂载弹窗组件
    const modalApp = createApp(SearchDialog);
    const modalInstance = modalApp.mount(document.createElement("div"));
    modalRef.value = modalInstance;

    document.body.appendChild(modalInstance.$el);
  },
};
